package com.xca.gsondemo.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xca.gsondemo.R;
import com.xca.gsondemo.entity.Olympic;

import java.util.ArrayList;
import java.util.List;

public class MedalsAdapter extends ArrayAdapter<Olympic> {

    private List<Olympic> olympicList;
    private LayoutInflater inflater;
    private int resourceLayout;

    public MedalsAdapter(@NonNull Context context, int resource, List<Olympic> objects) {
        super(context, resource, objects);
        this.olympicList = new ArrayList<>(objects);
        this.resourceLayout = resource;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(resourceLayout, parent, false);
            viewHolder.id = convertView.findViewById(R.id.c_id);
            viewHolder.name = convertView.findViewById(R.id.c_name);
            viewHolder.gold = convertView.findViewById(R.id.c_j);
            viewHolder.silver = convertView.findViewById(R.id.c_y);
            viewHolder.bronze = convertView.findViewById(R.id.c_t);
            viewHolder.count = convertView.findViewById(R.id.c_count);
            viewHolder.rank = convertView.findViewById(R.id.c_rank);

            convertView.setTag(viewHolder);

            // 设置点击监听器
            ViewHolder finalViewHolder = viewHolder;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String click_id = finalViewHolder.id.getText().toString();
                    for (Olympic olympic : olympicList){
                        if (click_id.equals(olympic.getCountryid())){
                            showDetailDialog(olympic);
                        }
                    }
                }
            });

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Olympic olympic = olympicList.get(position);
        if (olympic != null) {
            viewHolder.id.setText(olympic.getCountryid());
            viewHolder.name.setText(olympic.getCountryname());
            viewHolder.gold.setText(olympic.getGold());
            viewHolder.silver.setText(olympic.getSilver());
            viewHolder.bronze.setText(olympic.getBronze());
            viewHolder.count.setText(olympic.getCount());
            viewHolder.rank.setText(olympic.getRank());
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return olympicList.size();
    }

    @Nullable
    @Override
    public Olympic getItem(int position) {
        return olympicList.get(position);
    }

    private static class ViewHolder {
        TextView id;
        TextView name;
        TextView gold;
        TextView silver;
        TextView bronze;
        TextView count;
        TextView rank;
    }

    private void showDetailDialog(Olympic olympic) {
        // 创建一个AlertDialog来显示item的详细信息
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View dialogView = inflater.inflate(R.layout.dialog_item_detail, null);

        // 找到dialog中的TextView
        TextView idTextView = dialogView.findViewById(R.id.dialog_id);
        TextView nameTextView = dialogView.findViewById(R.id.dialog_name);
        TextView goldTextView = dialogView.findViewById(R.id.dialog_gold);
        TextView silverTextView = dialogView.findViewById(R.id.dialog_silver);
        TextView bronzeTextView = dialogView.findViewById(R.id.dialog_bronze);
        TextView countTextView = dialogView.findViewById(R.id.dialog_count);
        TextView rankTextView = dialogView.findViewById(R.id.dialog_rank);

        // 设置文本值
        idTextView.setText(olympic.getCountryid());
        nameTextView.setText(olympic.getCountryname());
        goldTextView.setText(olympic.getGold());
        silverTextView.setText(olympic.getSilver());
        bronzeTextView.setText(olympic.getBronze());
        countTextView.setText(olympic.getCount());
        rankTextView.setText(olympic.getRank());

        // 设置dialog的内容视图
        builder.setView(dialogView)
                .setTitle("详细信息")
                .setPositiveButton(android.R.string.ok, null);

        // 显示dialog
        builder.create().show();
    }
}